//
//  AIGirlMeansSliderView.swift
//  AIGirl
//
//  Created by Rae on 2023/12/27.
//

import UIKit

class AIGirlMeansSliderView: UIView {

    private lazy var sliderColorView        = c_make_ex(UIView.self)
        .bgColor("#FFFFFF".toColor.alpha(0.1)).corner(2.5).end()
    private lazy var sliderValueView        = c_make_ex(UIView.self)
        .bgColor("#F2F2F2".toColor.alpha(0.69)).corner(2.5).end()

    var value: Int = 0 {
        didSet {
            self.changeValue()
        }
    }
    
    override init(frame: CGRect) {
        super.init(frame: frame)

        backgroundColor = .clear
        subviews {
            sliderColorView.subviews(sliderValueView)
        }
        sliderColorView.leading(0).trailing(0).bottom(0).height(4.5)
        sliderValueView.leading(0).top(0).bottom(0).width(0)
    }
    
    @objc private func changeValue() {
        if sliderColorView.frame.isEmpty {
            perform(#selector(changeValue), with: nil, afterDelay: 0.15)
            return
        }
        let itemWidth: CGFloat = sliderColorView.frame.width / 100
        let viewWidth = itemWidth * CGFloat(value)
        sliderValueView.widthConstraint?.constant = viewWidth
        UIView.animate(withDuration: 0.35) {
            self.layoutIfNeeded()
        }
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }

}

class AIGirlNameView: UIView {
    
    private lazy var titleLabel = c_make_ex(UILabel.self)
        .color(.white).font(17.font_medium).alignment(.center).end()
    private lazy var nameTextField = c_make_ex(UITextField.self)
        .bgColor("#F2F2FD".toColor.alpha(0.2)).corner(22)
        .color(.white).font(14.font_medium).alignment(.center)
        .placeholder("Enter your nicknime").end()
    private lazy var nextButton = c_make_ex(AIGirlSubmitButton.self)
        .color(.white).font(21.font_bold).corner(20)
        .text("Next").end()
    var nextClosure: ((_ nameStr: String) -> Void)?
    var observer: NSObjectProtocol!
    
    var title: String = ""{
        didSet{
            titleLabel.text = title
        }
    }
    
    var placeholder: String = ""{
        didSet{
            nameTextField.placeholder = placeholder
        }
    }
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        
        subviews{
            titleLabel
            nameTextField
            nextButton
        }
        
        titleLabel.leading(15).trailing(15).top(65)
        nameTextField.leading(37.5).trailing(37.5).height(44)
        nameTextField.Top == titleLabel.Bottom + 20
        nextButton.leading(37.5).trailing(37.5).height(50)
        nextButton.Top == nameTextField.Bottom + 75
        
        nameTextField.placehold("#B6AFC4".toColor, font: 14.font_medium)
        nextButton.addTarget(self, action: #selector(clickNextButton), for: .touchUpInside)
        
        observer = NotificationCenter.default.addObserver(forName: UITextField.textDidChangeNotification, object: nameTextField, queue: .main) { [weak self] notification in
            guard let strongSelf = self else { return }
            if let text = strongSelf.nameTextField.text, text.count > 20 {
                strongSelf.nameTextField.text = String(text.prefix(20)) // 截断过长的文本
            }
        }
    }
    
    @objc private func clickNextButton(){
        
        if let name = nameTextField.text, name.count > 0 {
            nextClosure?(name)
        }else{
            "Please set a name!".toast_error
        }
    }
     
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    
}

class AIGirlInterestItem: UICollectionViewCell {
    
    private let contentV = UIButton(type: .custom)
    
    var model: AIGirlInterestModel? {
        didSet{
            guard let md = model else { return }
            contentV.isSelected = md.isSel
            contentV.setTitle(md.name, for: .normal)
        }
    }
    
    var msgModel: AIGirlUserInterestItemModel?{
        didSet{
            guard let md = msgModel else { return }
            contentV.isSelected = false
            contentV.setTitle(md.name, for: .normal)
        }
    }
        
    override init(frame: CGRect) {
        super.init(frame: frame)
        
        contentView.subviews {
            contentV
        }

        contentV.fillContainer()
        
        contentV.style { v in
            v.setTitleColor(.white, for: .normal)
            v.setTitleColor("#1C1B3B".toColor, for: .selected)
            v.titleLabel?.font = 16.font_medium
            v.setBackgroundImage(UIImage.colorToImage(color: "#F2F2FD".toColor.alpha(0.2)), for: .normal)
            v.setBackgroundImage(UIImage.colorToImage(color: .white), for: .selected)
            v.layer.cornerRadius = 20
            v.layer.masksToBounds = true
            v.isUserInteractionEnabled = false
        }
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
}
